{% extends 'base.html' %}
{% block title %}Role{% endblock %}

{% block card %}
<div class="layui-card-header">
  <h2>Role</h2>
  <a class="layui-btn layui-btn-lg" style="float:right;margin-top:-40px;margin-right:40px;"
     href="{{ url_for('main.hs_role_tree') }}">
    <i class="layui-icon">&#xe668;</i>查看列表
  </a>
</div>
{% endblock %}


{% block body %}
<form class="layui-form" method="post" id="MainForm">

  <div class="layui-form-item">
    <label class="layui-form-label">ID</label>
    <div class="layui-input-block">
      {{ form.record_id(class="layui-input layui-disabled", style="width:40%", readonly=1, disable=1, value=record.id)
      }}
    </div>
  </div>

  <div class="layui-form-item">
    <label class="layui-form-label">Name</label>
    <div class="layui-input-block">
      {{ form.name(class="layui-input", style="width:40%;", value=record.name) }}
    </div>
  </div>

  <div class="layui-form-item">
    <div class="layui-input-block">
      {{ form.submit(class="layui-btn", style="width:100px;font-size:medium", id="upsert") }}
      {{ form.new_again(class="layui-btn", style="width:100px;font-size:medium", id="new_again") }}
    </div>
  </div>


</form>
{% endblock %}

{% block script %}

<script type="text/javascript">
    layui.use(['form', 'jquery', 'upload'], function () {
        var form = layui.form;
        var $ = layui.$;

        $(document).ready(function(){

            $('#new_again').on('click',function () {
                window.location.href = "{{ url_for('main.hs_role_form', record_id=0) }}";
                return false;
            });

            $('#upsert').on('click',function () {

                var form_data = new FormData(document.getElementById("MainForm"));

                $.ajax({
                    url: "{{url_for('main.role_create_update')}}",
                    type: "post",
                    data: form_data,
                    processData: false,
                    contentType: false,
                    success: result => {

                        if(!validate_api_result(result)){return}; // 此处先预处理一下返回值, 需要报错的要报错

                        if(form_data.get('record_id')){
                            layui.layer.msg('更新成功!',{time: 1000});
                        }else{
                            layui.layer.msg('创建成功!',{time: 1000});
                        };

                        let data = result.data;
                        $("#record_id").val(data.record_id);

                    },
                    fail: err => {
                        layui.layer.msg('失败',{time: 1000});
                    }
                });
                return false;

            });
        });

    });



</script>


{% endblock %}